﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Threading;
using WorkFlowSystem.DataBase;
using WorkFlowSystem.Common;

namespace WorkFlowSystem
{
    public partial class IpSetting : Form
    {
        public IpSetting()
        {
            InitializeComponent();
        }

        private void buttonX1_Click(object sender, EventArgs e)
        {
            string ip = ipAddressInput1.Text.ToString();

            if (ip.Length == 0)
            {
                MessageBox.Show("IP不能为空。");
                return;
            }

            richTextBoxEx1.Clear();
            richTextBoxEx1.Refresh();
            Thread.Sleep(1000);

            richTextBoxEx1.Text = "开始连接测试。\r\n正在验证主机IP通信。\r\n";
            richTextBoxEx1.Refresh();
            Thread.Sleep(1000);

            if (Ping(ip))
            {
                richTextBoxEx1.Text += "IP通信连接成功！\r\n";
                richTextBoxEx1.Refresh();
                Thread.Sleep(1000);

                richTextBoxEx1.Text += "正在连接数据库。\r\n";
                richTextBoxEx1.Refresh();
                Thread.Sleep(1000);

                DBOperator dbo = new DBOperator();
                if (dbo.ConnectDB(ip) == 0)
                {
                    richTextBoxEx1.Text += "连接数据库成功。\r\n";
                    richTextBoxEx1.Refresh();
                    Thread.Sleep(1000);

                    if (SetDbIp(ip))
                    {
                        richTextBoxEx1.Text += "数据库IP设定成功。\r\n";


                    } else {
                        richTextBoxEx1.Text += "数据库IP设定失败。\r\n";

                    }

                    return;
                }
                else
                {
                    richTextBoxEx1.Text += "连接数据库失败。\r\n";
                    return;
                }

            }
            else
            {
                richTextBoxEx1.Text += "IP通信连接失败！\r\n";
                return;
            }

        }

        private bool Ping(string ip)
        {
            System.Net.NetworkInformation.Ping p = new System.Net.NetworkInformation.Ping();

            System.Net.NetworkInformation.PingOptions options = new System.Net.NetworkInformation.PingOptions();

            options.DontFragment = true;

            string data = "TEST DATA";

            byte[] buffer = Encoding.ASCII.GetBytes(data);

            int timeout = 1000;

            System.Net.NetworkInformation.PingReply reply = p.Send(ip, timeout, buffer, options);

            if (reply.Status == System.Net.NetworkInformation.IPStatus.Success)
            {
                return true;
            }
            else
            {
                return false;
            }

        }

        private bool SetDbIp(string ip)
        {
            ConfigOperator config = new ConfigOperator();
            if (config.SetStrValue("appSettings", "data_source", ip) < 0)
            {
                return false;
            }
            else
            {
                return true;
            }

        }


    }
}
